package cn.g31010.sip.service
{
	import cn.g31010.sip.model.*;
	import cn.g31010.sip.vo.User;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	
	public class UserManageService
	{
		private static var authservice:UserManageService;
		private var um:UserModel=UserModel.getInstance();
		public function UserManageService()
		{
			if(UserManageService.authservice!=null)
				throw new Error("Only one instance should be instantiated");
		}
		
		public static function getInstance():UserManageService{
			if(authservice==null){
				authservice=new UserManageService;
			}
			return authservice;
		}
		
		public function auth(name:String,passwd:String,callback:Function=null):void{
			CursorManager.setBusyCursor();
			var client:HessianClient=new HessianClient("AuthService",callback==null?authResp:callback);
				client.request("auth",name,passwd);
		}
		private function authResp(obj:Object):void{
			 var user:User = User.create(obj);
               if(user!=null){        
               	um.currentUser=user;		
               }else{
               		Alert.show("用户名或密码错误","登陆失败");
               }
              CursorManager.removeBusyCursor();
		}
		public function addUser(user:User,callback:Function=null):void{
			var client:HessianClient=new HessianClient("UserManageService",callback==null?addUserResp:callback);
			client.request("addUser",user);
		}
		private function addUserResp(obj:Object):void{
			var user:User = User.create(obj);
               if(user!=null){
              		Alert.show(user.id+"恭喜您，注册成功");
               }else{
               		Alert.show("用户名不合法","注册失败");
               }
               CursorManager.removeBusyCursor();
		}
		public function setUser(user:User,callback:Function=null):void{
			var client:HessianClient=new HessianClient("UserManageService",callback==null?setUserResp:callback);
			client.request("setUser",user);
		}
		private function setUserResp(obj:Object):void{
			var user:User = User.create(obj);
               if(user!=null){
               		um.removeUser(user.id);
               		um.addUser(user);
              		Alert.show("更新成功");
               }else{
               		Alert.show("用户名不合法","更新失败");
               }
               CursorManager.removeBusyCursor();
		}
		public function removeUser(id:String,callback:Function=null):void{
			var client:HessianClient=new HessianClient("UserManageService",callback==null?removeUserResp:callback);
			client.request("removeUser",id);
		}
		private function removeUserResp(obj:Object):void{
			var user:User = User.create(obj);
               if(user!=null){
               		um.removeUser(user.id);
              		Alert.show(user.id+"，删除成功");
              		
               }else{
               		Alert.show("用户名不合法","删除失败");
               }
               CursorManager.removeBusyCursor();
		}
	
		public function getAllUser():ArrayCollection{
			return null;
		}
	
		public function findUserById(id:String,callback:Function=null):void{
			var client:HessianClient=new HessianClient("UserManageService",callback==null?findUserByIdResp:callback);
			client.request("findUserById",id);
		}
		private function findUserByIdResp(obj:Object):void{
			var u:User=User.create(obj);
			if(u!=null){
				um.removeUser(u.id);
				um.addUser(u);
			}
             CursorManager.removeBusyCursor();
		}
	
		public function findUserByName(name:String,callback:Function=null):void{
			var client:HessianClient=new HessianClient("UserManageService",callback==null?findUserByNameResp:callback);
			client.request("findUserByName",name);
		}
		private function findUserByNameResp(obj:Object):void{
			var a:Array = obj as Array;
			for(var i:int=0;i<a.length;i++){
				var u:User=User.create(a[i]);
				um.users.addItem(u);
			}
            CursorManager.removeBusyCursor();
		}
	
		public function findUserByNickName(nickName:String,callback:Function=null):void{
			var client:HessianClient=new HessianClient("UserManageService",callback==null?findUserByNickNameResp:callback);
			client.request("findUserByNickName",nickName);
		}
		private function findUserByNickNameResp(obj:Object):void{
			var a:Array = obj as Array;
			for(var i:int=0;i<a.length;i++){
				var u:User=User.create(a[i]);
				um.users.addItem(u);
			}
            CursorManager.removeBusyCursor();
		}
	}
}